<template>
  <!-- page: Signup -->
  <div class = "signup-page mx-auto p-3 w-330">
    <h5 class = "my-4 text-center">注册知乎账户</h5>
    <validate-form v-on:form-submit="onFormSubmit">
      <div class = "mb-3">
        <label class = "form-label">Email:</label>
        <validate-input
          type = "text"
          placeholder = "请输入邮箱地址"
          v-bind:rules="emailRules"
          v-model="formData.email"
        />
      </div>
      <div class = "mb-3">
        <label class = "form-label">nickName:</label>
        <validate-input
          type = "text"
          placeholder = "请输入昵称"
          v-bind:rules="nameRules"
          v-model="formData.nickName"
        />
      </div>
      <div class = "mb-3">
        <label class = "form-label">Password:</label>
        <validate-input
          type = "password"
          placeholder = "请输入密码"
          v-bind:rules="passwordRules"
          v-model="formData.password"
        />
      </div>
      <div class = "mb-3">
        <label class = "form-label">Repeat the password:</label>
        <validate-input
          type = "password"
          placeholder = "请再次密码"
          v-bind:rules="repeatPasswordRules"
          v-model="formData.repeatPassword"
        />
      </div>
      <template #submit>
        <button type = "submit" class="btn btn-primary btn-block btn-large">注册新用户</button>
      </template>
    </validate-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue'
// 导入 vue-router
import { useRouter } from 'vue-router'
// 导入 axios
import axios from 'axios'
// 导入组件
import ValidateInput, { RulesProp } from '@/components/ValidateInput.vue'
import ValidateForm from '@/components/ValidateForm.vue'
// 导入 消息提示
import createMessage from '@/assets/js/createMessage'

// 对setup函数进行封装，返回options的对象
export default defineComponent({
  // 页面名称
  name: 'Signup',
  // 注册组件
  components: {
    ValidateInput,
    ValidateForm
  },

  setup () {
    const formData = reactive({
      email: '',
      nickName: '',
      password: '',
      repeatPassword: ''
    })
    const router = useRouter()
    // 邮箱验证规则
    const emailRules: RulesProp = [
      { type: 'required', message: '电子邮箱地址不能为空' },
      { type: 'email', message: '请输入正确的电子邮箱格式' }
    ]
    // 姓名验证规则
    const nameRules: RulesProp = [
      { type: 'required', message: '昵称不能为空' }
    ]
    // 密码验证规则
    const passwordRules: RulesProp = [
      { type: 'required', message: '密码不能为空' }
    ]
    // 重复密码验证规则
    const repeatPasswordRules: RulesProp = [
      { type: 'required', message: '重复密码不能为空' },
      {
        type: 'custom',
        validator: () => {
          return formData.password === formData.repeatPassword
        },
        message: '密码不相同'
      }
    ]
    // 注册账户
    const onFormSubmit = (result: boolean) => {
      if (result) {
        // 获取注册账户的数据
        const payload = {
          email: formData.email,
          password: formData.password,
          nickName: formData.nickName
        }
        // 账户注册成功 跳转回登录页
        axios.post('/users/', payload).then(data => {
          createMessage('注册成功, 2秒后跳转回登录页', 'success')
          setTimeout(() => {
            router.push('/login')
          }, 2000)
        }).catch(e => {
          console.log(e)
        })
      }
    }
    return {
      emailRules,
      nameRules,
      passwordRules,
      repeatPasswordRules,
      onFormSubmit,
      formData
    }
  }
})
</script>

<style lang="css" scoped>
.w-330 {
  max-width: 330px;
}
</style>
